import 'package:flutter/material.dart';
import 'package:flutter_trip/base_view/base_appbar.dart';


class Myrefresh extends StatefulWidget {
  @override
  _MyrefreshState createState() => _MyrefreshState();
}

class _MyrefreshState extends State<Myrefresh> {
  List<String> cityNames = [ '北京', '上海', '广州', '深圳', '杭州', '苏州', '成都', '武汉', '郑州', '洛阳', '厦门', '青岛', '拉萨' ];

  ScrollController _scrollController = ScrollController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController.addListener(() {
      if(_scrollController.position.pixels == _scrollController.position.maxScrollExtent){
        _loadData();
        print('触发加载更多');
      }

    });
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _scrollController.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppbarLeading('刷新',ElevatedButton(onPressed: (){
        Navigator.pop(context);
      }, child: Icon(Icons.arrow_back)),
      ),
      body: RefreshIndicator(

        onRefresh: _handleRefresh,
        child: ListView(
          controller: _scrollController,

          children: _buildList(),

      ),)

    );
  }
   Future<Null> _handleRefresh()async{

    await Future.delayed(Duration(seconds: 2));
    setState(() {
      cityNames = cityNames.reversed.toList();

    });
    return null;
  }

  List<Widget>_buildList(){
    return cityNames.map((e) => _item(e)).toList();
  }

  Widget _item(String city){
    return Container(
      height: 80,
      margin: EdgeInsets.only(bottom: 5),
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.teal),
      child: Text(city,style: TextStyle(color: Colors.white,fontSize: 20),),
    );
  }
  _loadData()async{
    await Future.delayed(Duration(seconds: 5));
    setState(() {
      List<String> list = List<String>.from(cityNames);
      list.addAll(cityNames);
      cityNames = list;
    });

  }

}
